const mongoose = require('mongoose');

// MongoDB 连接字符串
const dbURI = 'mongodb://127.0.0.1:27017/shoppings';

// 设置 strictQuery 为 false
mongoose.set('strictQuery', false);

// 连接数据库
mongoose.connect(dbURI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

const db = mongoose.connection;

// 监听连接成功
db.on('connected', () => {
    console.log('MongoDB 数据库连接成功');
});

// 监听连接错误
db.on('error', (err) => {
    console.error('MongoDB 数据库连接失败:', err);
});

// 监听连接断开
db.on('disconnected', () => {
    console.log('MongoDB 数据库连接已断开');
});

module.exports = mongoose;